#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "transmite.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务监控根目录");

ServiceManager::ptr sm;

void string_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        LOG_ERROR("获取通信管道失败");
        return;
    }

    lch_im::MsgTransmitService_Stub stub(channel.get());
    lch_im::NewMessageReq req;
    lch_im::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(lch_im::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
//      if (cntl.Failed() == true || rsp.success() == false) {
//              LOG_ERROR(" xiaoxi子服务调用失败：{}",cntl.ErrorText());
//          }
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void image_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        LOG_ERROR("获取通信管道失败");
        return;
    }

    lch_im::MsgTransmitService_Stub stub(channel.get());
    lch_im::NewMessageReq req;
    lch_im::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(lch_im::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        LOG_ERROR("获取通信管道失败");
        return;
    }

    lch_im::MsgTransmitService_Stub stub(channel.get());
    lch_im::NewMessageReq req;
    lch_im::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(lch_im::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_speech_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string &uid, const std::string &sid,
                  const std::string &filename, const std::string &content)
{
     auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        LOG_ERROR("获取通信管道失败");
        return;
    }

    lch_im::MsgTransmitService_Stub stub(channel.get());
    lch_im::NewMessageReq req;
    lch_im::GetTransmitTargetRsp rsp;
    req.set_request_id(uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(lch_im::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    sm = std::make_shared<ServiceManager>();
    sm->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline,sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline,sm.get(), std::placeholders::_1, std::placeholders::_2);
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

//    string_message("a40a-8c284cba-0000", "会话ID1", "吃饭了吗？");
//    string_message("afb0-40d41359-0000", "会话ID1", "吃的盖浇饭！！");
     image_message("a40a-8c284cba-0000", "会话ID1", "可爱表情图片数据");
     speech_message("afb0-40d41359-0000", "会话ID1", "动听猪叫声数据");
     file_message("a40a-8c284cba-0000", "会话ID1", "猪爸爸的文件名称", "猪爸爸的文件数据");

    return 0;
}
